//
//  ProductXsViewController.m
//  CMVRFForIOS
//
//  Created by zhangnorman on 15/12/27.
//  Copyright © 2015年 hvacapp. All rights reserved.
//

#import "ProductXsViewController.h"
#import "GuidData.h"
#import "ProjectData.h"
#import "ProductListViewController.h"
#import "CacheData.h"

@interface ProductXsViewController ()

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation ProductXsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"型式列表";
    [self initData];
    [self setupViews];
}

/**
 *  初始化数据
 */
- (void)initData {
    self.dataSource = [NSMutableArray new];
}

#pragma mark - 初始化视图
- (void)setupViews{
    self.tableView = [[UITableView alloc] initWithFrame:[UIView frameWithOutNav] style:UITableViewStylePlain];
    self.tableView.backgroundColor = CM_TABLE_BG_COLOR;
    [self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

#pragma mark - 加载数据
- (void)loadCacheData {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:self.tag forKey:@"tag"];
    [param setValue:self.xl forKey:@"xl"];
    [param setValue:[NSNumber numberWithInt:1] forKey:@"ac"]; //0 仅包括未上市  1 仅包括已上市 2 已上市未上市都包括
    self.dataSource = [[CacheData sharedDataBase]getArrayResponseWithUserName:[[AccountManager manager]token] andUrl:HTTP_GET_PROJECT_SERIES_XS andParam:[param stringFromDictionary] andModel:NSStringFromClass([GuidData class])];
    if (self.dataSource.count > 0) {
        [self.tableView reloadData];
    }
}
/**
 *  加载视图所有的数据
 */
- (void)loadData {
    //[self loadNationData];
    [self loadProductXs:self.tag];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self loadCacheData];
    [self loadData];
}


/**
 *  获得可选得产品系列列表
 *
 *  @param tag
 */
- (void)loadProductXs:(NSString *)tag {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:tag forKey:@"tag"];
    [param setValue:self.xl forKey:@"xl"];
    [param setValue:[NSNumber numberWithInt:1] forKey:@"ac"]; //0 仅包括未上市  1 仅包括已上市 2 已上市未上市都包括
    
    [HttpClient getWithUrl:HTTP_GET_PROJECT_SERIES_XS token:[[AccountManager manager]token] paramDic:param resultClass:[GuidData class] success:^(GuidData *obj) {
        if (obj.getProductXssResult != nil && obj.getProductXssResult.count > 0) {
            //写人缓存数据
            if ([[CacheData sharedDataBase] setResponseWithUserName:[[AccountManager manager]token] andUrl:HTTP_GET_PROJECT_SERIES_XS andParam:[param stringFromDictionary] andHours:1000 andResponse:obj.getProductXssResult]) {
                [self.dataSource removeAllObjects];
                [self.dataSource addObjectsFromArray:obj.getProductXssResult];
                [self.tableView reloadData];
            }
        }
        CMLog(@"%@", obj);
    } failure:^(NSError *error) {
        CMLog(@"%@", error);
    }];
}


#pragma mark - 事件响应

#pragma mark - UIAlertViewDelegate 代理

#pragma mark - tableView显示设置数据
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"XLCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.textColor = CM_TABLE_TITLE_COLOR;
    cell.detailTextLabel.textColor = CM_TABLE_INFO_COLOR;
    GuidData *item = [self.dataSource objectAtIndex:indexPath.row];
    cell.textLabel.text = item.name;
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    GuidData *item = [self.dataSource objectAtIndex:indexPath.row];
    //跳转到产品列表
    ProductListViewController *view = [ProductListViewController new];
    [self.navigationController pushViewController:view animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

